class mgrec_child_iter():
	def __init__(self, rec):
		self.rec = rec
		
	def __iter__(self):
		self.current = mgGetChild(self.rec)
		return self
		
	def next(self):
		if self.current:
			rec = self.current
			self.current = mgGetNext(rec)
			return rec
		else:
			raise StopIteration
			
class mgrec_child_reverse_iter():
	def __init__(self, rec):
		self.rec = rec
		
	def __iter__(self):
		n = mgCountChild(self.rec)
		if n > 0:
			self.current = mgGetChildNth(self.rec, n)
		else:
			self.current = None
		return self
		
	def next(self):
		if self.current:
			rec = self.current
			self.current = mgGetPrevious(rec)
			return rec
		else:
			raise StopIteration
			
class mgrec_nested_child_iter():
	def __init__(self, rec):
		self.rec = rec
		
	def __iter__(self):
		self.current = mgGetNestedChild(self.rec)
		return self
		
	def next(self):
		if self.current:
			rec = self.current
			self.current = mgGetNext(rec)
			return rec
		else:
			raise StopIteration
			
class mgrec_nested_child_reverse_iter():
	def __init__(self, rec):
		self.rec = rec
		
	def __iter__(self):
		self.current = mgGetNestedChild(self.rec)
		next_rec = mgGetNext(self.current)
		while next_rec:
			self.current = next_rec
			next_rec = mgGetNext(next_rec)
		return self
		
	def next(self):
		if self.current:
			rec = self.current
			self.current = mgGetPrevious(rec)
			return rec
		else:
			raise StopIteration
		
class mgrec_iter():
	def __init__(self, rec):
		self.rec = rec
		
	def __iter__(self):
		self.current = self.rec
		return self
		
	def next(self):
		if self.current:
			rec = self.current
			self.current = mgGetNext(rec)
			return rec
		else:
			raise StopIteration
			
class mgrec_reverse_iter():
	def __init__(self, rec):
		self.rec = rec
		
	def __iter__(self):
		self.current = self.rec
		return self
		
	def next(self):
		if self.current:
			rec = self.current
			self.current = mgGetPrevious(rec)
			return rec
		else:
			raise StopIteration
			
class mgrec_cyclic_iter():
	def __init__(self, rec):
		self.rec = rec
		
	def __iter__(self):
		self.current = self.rec
		self.first = self.rec
		prev_rec = mgGetPrevious(self.rec)
		while prev_rec:
			self.first = prev_rec
			prev_rec = mgGetPrevious(prev_rec)
		return self
		
	def next(self):
		if self.current:
			rec = self.current
			self.current = mgGetNext(rec)
			if self.current == None:
				self.current = self.first
			if self.current == self.rec:
				self.current = None
			return rec
		else:
			raise StopIteration
			
class mgrec_cyclic_reverse_iter():
	def __init__(self, rec):
		self.rec = rec
		
	def __iter__(self):
		self.current = self.rec
		self.last = self.rec
		next_rec = mgGetNext(self.rec)
		while next_rec:
			self.last = next_rec
			next_rec = mgGetNext(next_rec)
		return self
		
	def next(self):
		if self.current:
			rec = self.current
			self.current = mgGetPrevious(rec)
			if self.current == None:
				self.current = self.last
			if self.current == self.rec:
				self.current = None
			return rec
		else:
			raise StopIteration